﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.IO;

namespace TfsExtensions.Build.Activities
{
    public sealed class EnsureDirectoryStructureInWorkingDirectory : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<string> WorkingFolder { get; set; }
        public InArgument<string> ChmFolder { get; set; }
        public InArgument<string> IntellisenseFolderName { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            var workingFolder = this.WorkingFolder.Get(context);
            if (workingFolder != null)
            {
                if (!Directory.Exists(workingFolder))
                {
                    Directory.CreateDirectory(workingFolder);
                }
                
                if (!Directory.Exists(string.Format("{0}\\html", workingFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\html", workingFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\icons", workingFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\icons", workingFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\scripts", workingFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\scripts", workingFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\styles", workingFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\styles", workingFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\media", workingFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\media", workingFolder));
                }
            }

            var chmFolder = this.ChmFolder.Get(context);

            if (chmFolder != null)
            {
                if (!Directory.Exists(chmFolder))
                {
                    Directory.CreateDirectory(chmFolder);
                }

                if (!Directory.Exists(string.Format("{0}\\html", chmFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\html", chmFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\icons", chmFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\icons", chmFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\scripts", chmFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\scripts", chmFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\styles", chmFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\styles", chmFolder));
                }

                if (!Directory.Exists(string.Format("{0}\\media", chmFolder)))
                {
                    Directory.CreateDirectory(string.Format("{0}\\media", chmFolder));
                }
            }

            var intellisenseFolder = this.IntellisenseFolderName.Get(context);

            if (intellisenseFolder != null)
            {
                if (!Directory.Exists(intellisenseFolder))
                {
                    Directory.CreateDirectory(intellisenseFolder);
                }
            }
        }
    }
}
